package com.outsourcing.bottle.ui;

import java.util.HashMap;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.Html;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.db.BottleTypeTable;
import com.outsourcing.bottle.domain.BottleTagEntry;
import com.outsourcing.bottle.domain.BottleTagInfo;
import com.outsourcing.bottle.domain.BottleTypeEntry;
import com.outsourcing.bottle.domain.UrlConfig;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.BasicUIEvent;
import com.outsourcing.bottle.util.HttpUtils;
import com.outsourcing.bottle.util.ImageLoader;
import com.outsourcing.bottle.util.UICore;
import com.outsourcing.bottle.util.Utility;
/**
 * 
 * @author 06peng
 *
 */
public class ThrowBottleSettingActivity extends BasicActivity implements BasicUIEvent, OnClickListener, Callback {

	private final int init_bottle_info = 1;
	public static final int activity_for_result_choose_friend = 2;
	public static final int activity_for_result_bottle_tag = 3;
	int count = 0;
	
	private int bttype_id;
	private double bt_geo_lng; 
	private double bt_geo_lat; 
	private String reference;
	private BottleTypeEntry entry;
	private List<BottleTagEntry> tagEntrys;
	private BottleTagAdapter adapter;
	private Handler handler;
	
	private ImageView bottleImageView;
	private ImageView backView;
	private ImageButton confirmView;
	private TextView descriptionView;
	private TextView tipsView;
	private EditText tagView;
	private ImageButton wrapView;
	private ListView tagListView;
	
	private LinearLayout samecityLayout;
	private TextView samecityView;
	private ImageView samecityImage;
	private LinearLayout sameareaLayout;
	private TextView sameareaView;
	private ImageView sameareaImage;
	private LinearLayout genderLayout;
	private TextView genderView;
	private ImageView genderImage;
	private LinearLayout ageLayout;
	private TextView ageView;
	private ImageView ageImage;
	private LinearLayout pickLayout;
	private TextView pickView;
	private ImageView pickImage;
	private LinearLayout forwardLayout;
	private TextView forwardView;
	private ImageView forwarImage;
	private LinearLayout feedLayout;
	private TextView feedView;
	private ImageView feedImage;
	private LinearLayout commentLayout;
	private TextView commentView;
	private ImageView commentImage;
	private LinearLayout showaddressLayout;
	private TextView showaddressView;
	private ImageView showAddressImage;
	
	private String[] choicesAge;
	private String[] choicesGenger;
	private String[] choicesSamearea;
	private String[] choiceSamecity;
	private String[] choicesFeed;
	private String[] choicesPick;
	private String[] choicesReply;
	private String[] choicesForward;
	private String[] choicesAddress;
	
	private ImageLoader btConfigLoader;
	
	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		handler = new Handler(this);
		setContentView(R.layout.throw_bottle_setting);
		btConfigLoader = new ImageLoader(this, AppContext.BOTTLE_CONFIG);
		initBottleLocation();
		initWidget();
		networkLocat();
		UICore.eventTask(this, this, init_bottle_info, getString(R.string.init_data), null);
	}
	
	private void initWidget() {
		tagListView = (ListView) findViewById(R.id.throw_setting_tips_list);
		tagListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				BottleTagEntry tag = tagEntrys.get(arg2);
				if (tag != null) {
					tagView.setText(tag.getContent());
					tagView.setTag(tag.getTagid());
				}
			}
		});
		tagView = (EditText) findViewById(R.id.throw_setting_tag_edit);
		
		wrapView = (ImageButton) findViewById(R.id.throw_setting_wrap);
		wrapView.setOnClickListener(this);
		backView = (ImageView) findViewById(R.id.topbar_back);
		backView.setOnClickListener(this);
		bottleImageView = (ImageView) findViewById(R.id.throw_setting_bottle_image);
		confirmView = (ImageButton) findViewById(R.id.topbar_confirm);
		confirmView.setOnClickListener(this);
		descriptionView = (TextView) findViewById(R.id.throw_setting_bottle_desctiption);
		tipsView = (TextView) findViewById(R.id.throw_setting_bottle_tips);
		
		
		samecityLayout = (LinearLayout) findViewById(R.id.throw_setting_samecity_layout);
		samecityLayout.setOnClickListener(this);
		samecityView = (TextView) findViewById(R.id.throw_setting_samecity_require);
		sameareaLayout = (LinearLayout) findViewById(R.id.throw_setting_samearea_layout);
		sameareaLayout.setOnClickListener(this);
		sameareaView = (TextView) findViewById(R.id.throw_setting_samearea_require);
		genderLayout = (LinearLayout) findViewById(R.id.throw_setting_gender_layout);
		genderLayout.setOnClickListener(this);
		genderView = (TextView) findViewById(R.id.throw_setting_gender_require);
		ageLayout = (LinearLayout) findViewById(R.id.throw_setting_age_layout);
		ageLayout.setOnClickListener(this);
		ageView = (TextView) findViewById(R.id.throw_setting_age_require);
		pickLayout = (LinearLayout) findViewById(R.id.throw_setting_pick_layout);
		pickLayout.setOnClickListener(this);
		pickView = (TextView) findViewById(R.id.throw_setting_pick_enable);
		forwardLayout = (LinearLayout) findViewById(R.id.throw_setting_spread_layout);
		forwardLayout.setOnClickListener(this);
		forwardView = (TextView) findViewById(R.id.throw_setting_spread_enable);
		feedLayout = (LinearLayout) findViewById(R.id.throw_setting_feed_layout);
		feedLayout.setOnClickListener(this);
		feedView = (TextView) findViewById(R.id.throw_setting_feed);
		commentLayout = (LinearLayout) findViewById(R.id.throw_setting_comment_layout);
		commentLayout.setOnClickListener(this);
		commentView = (TextView) findViewById(R.id.throw_setting_comment);
		samecityImage = (ImageView) findViewById(R.id.samecity_image);
		sameareaImage = (ImageView) findViewById(R.id.samearea_image);
		genderImage = (ImageView) findViewById(R.id.samesex_image);
		ageImage = (ImageView) findViewById(R.id.sameage_image);
		pickImage = (ImageView) findViewById(R.id.allowpick_image);
		forwarImage = (ImageView) findViewById(R.id.allowspread_image);
		feedImage = (ImageView) findViewById(R.id.allowfeed_image);
		commentImage = (ImageView) findViewById(R.id.allowreplay_image);
		showaddressLayout = (LinearLayout) findViewById(R.id.throw_setting_showaddress_layout);
		showaddressLayout.setOnClickListener(this);
		showaddressView = (TextView) findViewById(R.id.throw_setting_showaddress_require);
		showAddressImage = (ImageView) findViewById(R.id.showaddress_image);
		showAddressImage.setBackgroundResource(R.drawable.set_samecity);
		showaddressView.setTag(1);
		
	}
	
	private void initBottleLocation() {
		Intent intent = getIntent();
		bttype_id = intent.getIntExtra("bttype_id", 0);
		bt_geo_lng = intent.getDoubleExtra("bt_geo_lng", 0);
		bt_geo_lat = intent.getDoubleExtra("bt_geo_lat", 0);
		reference = intent.getStringExtra("reference");
	}
	
	@Override
	public void onClick(View v) {
		if (v == wrapView) {
			if (tagListView.isShown()) {
				tagListView.setVisibility(View.GONE);
				wrapView.setBackgroundResource(R.drawable.unwrap);
			} else {
				tagListView.setVisibility(View.VISIBLE);
				wrapView.setBackgroundResource(R.drawable.wrap);
			}
		} else if (v == backView) {
			finish();
		} else if (v == confirmView) {
			Intent intent = new Intent(this, ExpandEditActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString("from_type", AppContext.SEND_BT);
			getSettingParams(bundle);
			intent.putExtras(bundle);
			startActivity(intent);
		} else if (v == samecityLayout) {
			doSettingAction(samecityView, choiceSamecity, getString(R.string.throw_bottle_setting_samecity_require));
		} else if (v == sameareaLayout) {
			doSettingAction(sameareaView, choicesSamearea, getString(R.string.throw_bottle_setting_samearea_require));
		} else if (v == genderLayout) {
			doSettingAction(genderView, choicesGenger, getString(R.string.throw_bottle_setting_gender_norequire));
		} else if (v == ageLayout) {
			doSettingAction(ageView, choicesAge, getString(R.string.throw_bottle_setting_age_require));
		} else if (v == pickLayout) {
			doSettingAction(pickView, choicesPick, getString(R.string.throw_bottle_setting_pickenable));
		} else if (v == forwardLayout) {
			doSettingAction(forwardView, choicesForward, getString(R.string.throw_bottle_setting_forwardenable));
		} else if (v == feedLayout) {
			doSettingAction(feedView, choicesFeed, getString(R.string.throw_bottle_setting_feed));
		} else if (v == commentLayout) {
			doSettingAction(commentView, choicesReply, getString(R.string.throw_bottle_setting_comment));
		} else if (v == showaddressLayout) {
			doSettingAction(showaddressView, choicesAddress, getString(R.string.throw_bottle_setting_showaddress_require));
		}
	}
	
	@Override
	public void execute(int mes, Object obj) {
		switch (mes) {
		case init_bottle_info:
			initBottleType();
			handler.sendEmptyMessage(init_bottle_info);
			break;

		default:
			break;
		}
	}

	private void initBottleType() {
		getTagsList();
		BottleTypeTable typeTable = new BottleTypeTable();
		entry = typeTable.getBottleType(bttype_id);
		choicesAge = new String[] {getString(R.string.throw_bottle_setting_age_lower16),
				getString(R.string.throw_bottle_setting_age_between16_22), 
				getString(R.string.throw_bottle_setting_age_between23_30), 
				getString(R.string.throw_bottle_setting_age_between31_40),
				getString(R.string.throw_bottle_setting_age_than41),
				getString(R.string.throw_bottle_setting_age_same), getString(R.string.throw_bottle_setting_age_norequire)};
		choicesGenger = new String[] {getString(R.string.throw_bottle_setting_gerder_same), 
				getString(R.string.throw_bottle_setting_gerder_different), getString(R.string.throw_bottle_setting_gender_norequire)};
		choiceSamecity = new String[] {getString(R.string.throw_bottle_setting_samecity_same), 
				getString(R.string.throw_bottle_setting_samecity_norequire)};
		choicesSamearea = new String[] {getString(R.string.throw_bottle_setting_samearea_same), 
				getString(R.string.throw_bottle_setting_samearea_norequire)};
		choicesPick = new String[] {getString(R.string.throw_bottle_setting_pick_enable), 
				getString(R.string.throw_bottle_setting_pick_disable)};
		choicesForward = new String[] {getString(R.string.throw_bottle_setting_forward_enable), 
				getString(R.string.throw_bottle_setting_forward_disable)};
		choicesFeed = new String[] {getString(R.string.throw_bottle_setting_feed_public), 
				getString(R.string.throw_bottle_setting_feed_private)};
		choicesReply = new String[] {getString(R.string.throw_bottle_setting_comment_public), 
				getString(R.string.throw_bottle_setting_comment_private)};
		choicesAddress = new String[] {getString(R.string.throw_bottle_setting_showaddress_show), 
				getString(R.string.throw_bottle_setting_showaddress_no)};
		
	}
	
	private void getTagsList() {
		String url = UrlConfig.get_bttags;
		HashMap<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("login_uid", String.valueOf(AppContext.getInstance().getLogin_uid()));
		paramsMap.put("login_token", AppContext.getInstance().getLogin_token());
		paramsMap.put("bttypeid", String.valueOf(bttype_id));
		try {
			String result = HttpUtils.doPost(this, url, paramsMap);
			if (result != null) {
				Gson gson = new Gson();
				BottleTagInfo info = gson.fromJson(result, BottleTagInfo.class);
				tagEntrys = info.getBttags_list();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void updateUI() {
		if (tagEntrys != null && !tagEntrys.isEmpty()) {
			tagListView.setVisibility(View.VISIBLE);
			adapter = new BottleTagAdapter();
			tagListView.setAdapter(adapter);
			Utility.setListViewHeightBasedOnChildren(tagListView);
			for (BottleTagEntry tag : tagEntrys) {
				if (tag.getIs_default() == 1) {
					tagView.setText(tag.getContent());
					tagView.setTag(tag.getTagid());
				}
			}
		}
		String tips = "<font color=red>"+getString(R.string.throw_bottle_setting_tips)+"</font>";
		if (AppContext.language == 1) {
			descriptionView.setText(Html.fromHtml("<font color=#419AD9>"+entry.getBttype_name_cn() + ":" +"</font>" + entry.getBttype_desc_cn()));
			tipsView.setText(Html.fromHtml(tips + entry.getBttype_tips_cn()));
		} else {
			descriptionView.setText(Html.fromHtml("<font color=#419AD9>"+entry.getBttype_name_en() + ":"+"</font>" + entry.getBttype_desc_en()));
			tipsView.setText(Html.fromHtml(tips + entry.getBttype_tips_en()));
		}
		btConfigLoader.DisplayImage(entry.getBttype_sjwicon(), bottleImageView, R.drawable.bottle_type_1);
		
		if (entry.getBttype_pick_show() == 1) {
			pickLayout.setVisibility(View.VISIBLE);
		} else {
			pickLayout.setVisibility(View.GONE);
		}
		if (entry.getBttype_spread_show() == 1) {
			forwardLayout.setVisibility(View.VISIBLE);
		} else {
			forwardLayout.setVisibility(View.GONE);
		}
		if (entry.getBttype_samecity_show() == 1) {
			samecityLayout.setVisibility(View.VISIBLE);
		} else {
			samecityLayout.setVisibility(View.GONE);
		}
		if (entry.getBttype_samearea_show() == 1) {
			sameareaLayout.setVisibility(View.VISIBLE);
		} else {
			sameareaLayout.setVisibility(View.GONE);
		}
		if (entry.getBttype_needage_show() == 1) {
			ageLayout.setVisibility(View.VISIBLE);
		} else {
			ageLayout.setVisibility(View.GONE);
		}
		if (entry.getBttype_needsex_show() == 1) {
			genderLayout.setVisibility(View.VISIBLE);
		} else {
			genderLayout.setVisibility(View.GONE);
		}
		if (entry.getBttype_otheruserfeed_show() == 1) {
			feedLayout.setVisibility(View.VISIBLE);
		} else {
			feedLayout.setVisibility(View.GONE);
		}
		if (entry.getBttype_otheruserreply_show() == 1) {
			commentLayout.setVisibility(View.VISIBLE);
		} else {
			commentLayout.setVisibility(View.VISIBLE);
		}
	}
	
	public class BottleTagAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return tagEntrys == null ? 0: tagEntrys.size();
		}

		@Override
		public Object getItem(int position) {
			return tagEntrys == null ? null : tagEntrys.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.simple_listitem, null);
			}
			TextView nameView = (TextView) convertView.findViewById(R.id.simple_listitem_name);
			BottleTagEntry entry = tagEntrys.get(position);
			if (entry != null) {
				nameView.setText(entry.getContent());
			}
			return convertView;
		}
		
	}
	
	private void doSettingAction(final TextView textView, final String[] actions, String title) {
		final Context dialogContext = new ContextThemeWrapper(this, android.R.style.Theme_Light);
		String cancel = getString(R.string.bt_title_back);
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, actions);
		final AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);
		builder.setTitle(title);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						int value = -1;
						if (which == actions.length - 1) {
							value = 0;
						} else {
							value = which + 1;
						}
						textView.setText(actions[which]);
						textView.setTag(value);
						changeSettingImageView(textView);
					}
				});
		builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
	
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
			
		});
		builder.create().show();
	}
	
	private void getSettingParams(Bundle bundle) {
		if (entry.getBttype_allow_pick() == 1) {
			if (pickView.getTag() != null) {
				int value = Integer.parseInt(pickView.getTag().toString());
				bundle.putInt("bt_allowpick", value);
			}
		} else {
			bundle.putInt("bt_allowpick", -1);
		}
		if (entry.getBttype_allow_spread() == 1) {
			if (forwardView.getTag() != null) {
				int value = Integer.parseInt(forwardView.getTag().toString());
				bundle.putInt("bt_allowspread", value);
			}
		} else {
			bundle.putInt("bt_allowspread", -1);
		}
		if (entry.getBttype_samecity_show() == 1) {
			if (samecityView.getTag() != null) {
				int value = Integer.parseInt(samecityView.getTag().toString());
				bundle.putInt("bt_samecity", value);
			}
		} else {
			bundle.putInt("bt_samecity", -1);
		}
		if (entry.getBttype_samearea_show() == 1) {
			if (sameareaView.getTag() != null) {
				int value = Integer.parseInt(sameareaView.getTag().toString());
				bundle.putInt("bt_samearea", value);
			}
		} else {
			bundle.putInt("bt_samearea", -1);
		}
		if (entry.getBttype_needage_show() == 1) {
			if (ageView.getTag() != null) {
				int value = Integer.parseInt(ageView.getTag().toString());
				bundle.putInt("bt_age", value);
			}
		} else {
			bundle.putInt("bt_age", -1);
		}
		if (entry.getBttype_needsex_show() == 1) {
			if (genderView.getTag() != null) {
				int value = Integer.parseInt(genderView.getTag().toString());
				bundle.putInt("bt_sex", value);
			}
		} else {
			bundle.putInt("bt_sex", -1);
		}
		if (entry.getBttype_otheruserfeed_show() == 1) {
			if (feedView.getTag() != null) {
				int value = Integer.parseInt(feedView.getTag().toString());
				bundle.putInt("bt_showfeed", value);
			}
		} else {
			bundle.putInt("bt_showfeed", -1);
		}
		if (entry.getBttype_otheruserreply_show() == 1) {
			if (commentView.getTag() != null) {
				int value = Integer.parseInt(commentView.getTag().toString());
				bundle.putInt("bt_showreply", value);
			}
		} else {
			bundle.putInt("bt_showreply", -1);
		}
		bundle.putString("bt_tagid", tagView.getTag().toString());
		bundle.putString("bt_geo_reference", reference);
		String showaddress = "0";
		if (showaddressView.getTag() != null) {
			showaddress = showaddressView.getTag().toString();
		}
		bundle.putString("bt_tag", tagView.getText().toString());
		bundle.putString("bt_showaddress", showaddress);
		bundle.putDouble("bt_geo_lat", bt_geo_lat);
		bundle.putDouble("bt_geo_lng", bt_geo_lng);
		bundle.putInt("bttype_id", bttype_id);
	}
	
	private void initSetting() {
		ageView.setText(choicesAge[entry.getBttype_needage() == 0 ? choicesAge.length - 1 : entry.getBttype_needage() - 1]);
		ageView.setTag(entry.getBttype_needage());
		
		genderView.setText(choicesGenger[entry.getBttype_needsex() == 0 ? choicesGenger.length - 1 : entry.getBttype_needsex() - 1]);
		genderView.setTag(entry.getBttype_needsex());
		
		commentView.setText(choicesReply[entry.getBttype_otheruserreply() == 0 ? choicesReply.length - 1 : entry.getBttype_otheruserreply() - 1]);
		commentView.setTag(entry.getBttype_otheruserreply());
		
		feedView.setText(choicesFeed[entry.getBttype_otheruserfeed() == 0 ? choicesFeed.length - 1 : entry.getBttype_otheruserfeed() - 1]);
		feedView.setTag(entry.getBttype_otheruserfeed());
		
		forwardView.setText(choicesForward[entry.getBttype_allow_spread() == 0 ? choicesForward.length - 1 : entry.getBttype_allow_spread() - 1]);
		forwardView.setTag(entry.getBttype_allow_spread());
		
		pickView.setText(choicesPick[entry.getBttype_allow_pick() == 0 ? choicesPick.length - 1 : entry.getBttype_allow_pick() - 1]);
		pickView.setTag(entry.getBttype_allow_pick());
		
		sameareaView.setText(choicesSamearea[entry.getBttype_samearea() == 0 ? choicesSamearea.length - 1 : entry.getBttype_samearea() - 1]);
		sameareaView.setTag(entry.getBttype_samearea());
		
		samecityView.setText(choiceSamecity[entry.getBttype_samecity() == 0 ? choiceSamecity.length - 1: entry.getBttype_samecity() - 1]);
		samecityView.setTag(entry.getBttype_samecity());
		if (entry.getBttype_needage() == 0) {
			ageImage.setBackgroundResource(R.drawable.set_sameage_gray);
		} else {
			ageImage.setBackgroundResource(R.drawable.set_sameage);
		}
		if (entry.getBttype_needsex() == 0) {
			genderImage.setBackgroundResource(R.drawable.set_samesex_gray);
		} else {
			genderImage.setBackgroundResource(R.drawable.set_samesex);
		}
	    if (entry.getBttype_otheruserreply() == 0) {
	    	commentImage.setBackgroundResource(R.drawable.set_allowreply_gray);
		} else {
			commentImage.setBackgroundResource(R.drawable.set_allowreply);
		}
		if (entry.getBttype_otheruserfeed() == 0) {
			feedImage.setBackgroundResource(R.drawable.set_allowfeed_gray);
		} else {
			feedImage.setBackgroundResource(R.drawable.set_allowfeed);
		}
		if (entry.getBttype_allow_spread() == 0) {
			forwarImage.setBackgroundResource(R.drawable.set_allowspread_gray);
		} else {
			forwarImage.setBackgroundResource(R.drawable.set_allowspread);
		}
		if (entry.getBttype_allow_pick() == 0) {
			pickImage.setBackgroundResource(R.drawable.set_allowpick_gray);
		} else {
			pickImage.setBackgroundResource(R.drawable.set_allowpick);
		}
	    if (entry.getBttype_samearea() == 0) {
	    	sameareaImage.setBackgroundResource(R.drawable.set_samearea_gray);
		} else {
			sameareaImage.setBackgroundResource(R.drawable.set_samearea);
		}
		if (entry.getBttype_samecity() == 0) {
			samecityImage.setBackgroundResource(R.drawable.set_samecity_gray);
		} else {
			samecityImage.setBackgroundResource(R.drawable.set_samecity);
		}
	}
	
	private void changeSettingImageView(TextView view) {
		if (view == ageView) {
			if (ageView.getTag() != null) {
				int value = Integer.parseInt(ageView.getTag().toString());
				if (value == 0) {
					ageImage.setBackgroundResource(R.drawable.set_sameage_gray);
				} else {
					ageImage.setBackgroundResource(R.drawable.set_sameage);
				}
			}
		} else if (view == genderView) {
			if (entry.getBttype_needsex_show() == 1) {
				if (genderView.getTag() != null) {
					int value = Integer.parseInt(genderView.getTag().toString());
					if (value == 0) {
						genderImage.setBackgroundResource(R.drawable.set_samesex_gray);
					} else {
						genderImage.setBackgroundResource(R.drawable.set_samesex);
					}
				}
			} 
			
		} else if (view == commentView) {
			if (commentView.getTag() != null) {
				int value = Integer.parseInt(commentView.getTag().toString());
				if (value == 0) {
					commentImage.setBackgroundResource(R.drawable.set_allowreply_gray);
				} else {
					commentImage.setBackgroundResource(R.drawable.set_allowreply);
				}
			}
		} else if (view == feedView) {
			if (feedView.getTag() != null) {
				int value = Integer.parseInt(feedView.getTag().toString());
				if (value == 0) {
					feedImage.setBackgroundResource(R.drawable.set_allowfeed_gray);
				} else {
					feedImage.setBackgroundResource(R.drawable.set_allowfeed);
				}
			}
		} else if (view == forwardView) {
			if (forwardView.getTag() != null) {
				int value = Integer.parseInt(forwardView.getTag().toString());
				if (value == 0) {
					forwarImage.setBackgroundResource(R.drawable.set_allowspread_gray);
				} else {
					forwarImage.setBackgroundResource(R.drawable.set_allowspread);
				}
			}
		} else if (view == pickView) {
			if (pickView.getTag() != null) {
				int value = Integer.parseInt(pickView.getTag().toString());
				if (value == 0) {
					pickImage.setBackgroundResource(R.drawable.set_allowpick_gray);
				} else {
					pickImage.setBackgroundResource(R.drawable.set_allowpick);
				}
			}
		} else if (view == sameareaView) {
			if (sameareaView.getTag() != null) {
				int value = Integer.parseInt(sameareaView.getTag().toString());
				if (value == 0) {
					sameareaImage.setBackgroundResource(R.drawable.set_samearea_gray);
				} else {
					sameareaImage.setBackgroundResource(R.drawable.set_samearea);
				}
			}
		} else if (view == samecityView) {
			if (samecityView.getTag() != null) {
				int value = Integer.parseInt(samecityView.getTag().toString());
				if (value == 0) {
					samecityImage.setBackgroundResource(R.drawable.set_samecity_gray);
				} else {
					samecityImage.setBackgroundResource(R.drawable.set_samecity);
				}
			}
		} else if (view == showaddressView) {
			if (showaddressView.getTag() != null) {
				int value = Integer.parseInt(showaddressView.getTag().toString());
				if (value == 0) {
					showAddressImage.setBackgroundResource(R.drawable.set_samecity_gray);
				} else {
					showAddressImage.setBackgroundResource(R.drawable.set_samecity);
				}
			}
		}
	}
	

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case init_bottle_info:
			updateUI();
			initSetting();
			break;

		default:
			break;
		}
		return false;
	}

}
